package com.xsky.logic.department;

import java.util.List;
import java.util.UUID;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabCorpDepa;

public class DepartmentLogic
{
	public String add(String xmlDoc)//增加一个文件栏目
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", TabCorpDepa.class);
		TabCorpDepa ft = (TabCorpDepa)xs.fromXML(xmlDoc);
		List rootList = baseDao.findByHql("from TabCorpDepa model where model.corpId='"+ft.getCorpId()+"' and model.farDeptId='"+0+"'");
		if(rootList.size()==0)
		{
			TabCorpDepa root = new TabCorpDepa();
			root.setCorpId(ft.getCorpId());
			root.setDeptName("根节点");
			root.setFarDeptId("0");
			root.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(root);
			ft.setId(UUID.randomUUID().toString().substring(0,31));
			ft.setFarDeptId(root.getId());
			ft.setFarDeptName(root.getDeptName());
			baseDao.insertEntity(ft);
		}
		else
		{
			if(ft.getFarDeptId()==null||"".equals(ft.getFarDeptId()))
			{
				TabCorpDepa tcd = (TabCorpDepa)rootList.get(0);
				ft.setFarDeptId(tcd.getId());
				ft.setFarDeptName(tcd.getDeptName());
			}
			ft.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(ft);
		}
		return "新增部门成功";
	}
	public String delete(String id)//删除一个文件栏目
	{
		String hql = "delete TabCorpDepa model where model.id='"+id+"'";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.execute(hql);
		baseDao.execute("delete TabCorpDepa model where model.farDeptId='"+id+"'");
		return "删除部门成功";
	}
	public String update(String xmlDoc)//更新一个文件栏目
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", TabCorpDepa.class);
		TabCorpDepa ft = (TabCorpDepa)xs.fromXML(xmlDoc);
		baseDao.updateEntity(ft);
		return "更新部门成功";
	}
	public String genTree(String id)//获取某个节点的部门树
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		TabCorpDepa entity = new TabCorpDepa();
		entity = (TabCorpDepa)baseDao.findByPrimaryKey(entity, "id", id);
		String childXml = "";
		
		if(entity.getDeptCode()==null||"null".equals(entity.getDeptCode())||"".equals(entity.getDeptCode()))
		{
			childXml+="<item id=\""+entity.getId()+"\" deptName=\""+entity.getDeptName()+"\" label=\""+entity.getDeptName()+"\" corpId=\""+entity.getCorpId()+"\" farDeptId=\""+entity.getFarDeptId()+"\" farDeptName=\""+entity.getFarDeptName()+"\">";
		}
		else
		{
			childXml+="<item id=\""+entity.getId()+"\" deptName=\""+entity.getDeptName()+"\" label=\""+entity.getDeptCode()+"|"+entity.getDeptName()+"\" corpId=\""+entity.getCorpId()+"\" farDeptId=\""+entity.getFarDeptId()+"\" farDeptName=\""+entity.getFarDeptName()+"\">";
		}
		List fileTitleList = baseDao.findByHql("from TabCorpDepa model where model.farDeptId='"+id+"'");
		for(int i = 0; i < fileTitleList.size(); i++)
		{
			TabCorpDepa childEntity = (TabCorpDepa)fileTitleList.get(i);
			childXml+=genTree(childEntity.getId());
		}
		childXml+="</item>";
		return childXml;
	}
	public String genTreeRootId(String companyId)
	{
		String id="0";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findByHql("from TabCorpDepa model where model.corpId='"+companyId+"' and model.farDeptId='0'");
		for(int i = 0; i < list.size(); i++)
		{
			TabCorpDepa childEntity = (TabCorpDepa)list.get(i);
			id = childEntity.getId();
		}
		return id;
	}
	public String isEmpty(String id)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List lmList = baseDao.findByHql("from TabCorpDepa model where model.farDeptId='"+id+"'");
		if(lmList.size()>0)
		{
			return "false";
		}
		return "true";
	}
}
